const app = getApp()
const dayjs = app.dayjs
const get_user_data = require('../../utils/get_user_data.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_dark: false,
    user_data: {},
    is_rank: false,
    score_list: [],
    //成绩math_model:[count,first_bit,second_bit,model]
    count_list: ['10个问题', '20个问题', '30个问题', '50个问题', '100个问题', ],
    first_bit_list: ['个位数', '两位数', '三位数', '四位数', '五位数'],
    second_bit_list: ['个位数', '两位数', '三位数', '四位数', '五位数'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },
  onShow() {
    // 检查是否登录过
    if (app.global_data.user_data.openid) {
      this.getUserData(()=>{})
    }
    this.setData({
      user_data: app.global_data.user_data,
      is_dark: app.global_data.darkness
    })
    this.getScoreRank()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  /* 
  朋友圈
  */
 onShareTimeline() {

},
  ...get_user_data.methods,
  /* 点击排行 */
  clickRank() {
    if (this.data.is_rank) {
      this.setData({
        is_rank: false
      })
    } else {
      this.setData({
        is_rank: true
      })
    }
  },
  /* model转文字 */
  mathModelLabel(array) {
    let label = this.data.first_bit_list[array[1]] + '和' + this.data.second_bit_list[array[2]] + '的' + this.data.count_list[array[0]]
    return label
  },
  /* 获取得分排名 */
  getScoreRank() {
    let is_day = dayjs(dayjs().format('YYYY-MM-DD')).valueOf()
    let after_day = is_day + 86399999
    wx.cloud.callFunction({
      name: 'runDb',
      data: {
        type: 'get_rank_list',
        collection: 'score_rank',
        is_day,
        after_day,
        skip: 0,
        limit: 10
      },
      complete: res => {
        // console.log(res)
        let list = res.result.data
        // console.log(list)
        list.forEach(el => {
          el.math_model_label = this.mathModelLabel(el.math_model)
        })
        this.setData({
          score_list: list
        })
      }
    })
  }
})